package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.work.domain.WorkCost;
import com.ruoyi.work.service.IWorkCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: MMDZ
 * @Desc: TODO 人力成本
 */
@RestController
@RequestMapping("/work/cost")
public class WorkCostController extends BaseController {

    @Autowired
    private IWorkCostService workCostService;

    /**
     * 查询人力成本信息列表
     */
    @PreAuthorize("@ss.hasPermi('work:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkCost workCost) {
        startPage();
        List<WorkCost> list = workCostService.selectWorkCostList(workCost);
        return getDataTable(list);
    }

    /**
     * 导出人力成本信息列表
     */
    @PreAuthorize("@ss.hasPermi('work:cost:export')")
    @Log(title = "人力成本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkCost workCost) {
        List<WorkCost> list = workCostService.selectWorkCostList(workCost);
        ExcelUtil<WorkCost> util = new ExcelUtil<WorkCost>(WorkCost.class);
        util.exportExcel(response, list, "人力成本信息数据");
    }

    /**
     * 人力成本信息模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WorkCost> util = new ExcelUtil<WorkCost>(WorkCost.class);
        util.importTemplateExcel(response, "人力成本数据");
    }

    /**
     * 导入人力成本信息
     */
    @Log(title = "导入人力成本信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('work:cost:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WorkCost> util = new ExcelUtil<WorkCost>(WorkCost.class);
        List<WorkCost> costList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = workCostService.importData(costList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 获取人力成本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:cost:query')")
    @GetMapping(value = "/{costId}")
    public AjaxResult getInfo(@PathVariable("costId") Long costId) {
        return AjaxResult.success(workCostService.selectWorkCostById(costId));
    }

    /**
     * 新增人力成本信息
     */
    @PreAuthorize("@ss.hasPermi('work:cost:add')")
    @Log(title = "人力成本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkCost workCost) {
        if (UserConstants.NOT_UNIQUE.equals(workCostService.checkUserUnique(workCost))) {
            return AjaxResult.error("新增人力成本信息'" + workCost.getUser().getNickName() + "'失败，人员已存在");
        }
        workCost.setCreateBy(getUsername());
        return toAjax(workCostService.insertWorkCost(workCost));
    }

    /**
     * 修改人力成本信息
     */
    @PreAuthorize("@ss.hasPermi('work:cost:edit')")
    @Log(title = "人力成本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkCost workCost) {
        if (UserConstants.NOT_UNIQUE.equals(workCostService.checkUserUnique(workCost))) {
            return AjaxResult.error("修改人力成本信息'" + workCost.getUserName() + "'失败，人员已存在");
        }
        workCost.setUpdateBy(getUsername());
        return toAjax(workCostService.updateWorkCost(workCost));
    }

    /**
     * 删除人力成本信息
     */
    @PreAuthorize("@ss.hasPermi('work:cost:remove')")
    @Log(title = "人力成本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{costIds}")
    public AjaxResult remove(@PathVariable Long[] costIds) {
        return toAjax(workCostService.deleteWorkCostByIds(costIds));
    }

}
